package thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPoolDemo {
    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newFixedThreadPool(2);//创建一个固定大小的线程池

        for (int i = 0; i < 5; i++){
            Runnable r=()->{
                try {
                    Thread t=Thread.currentThread();//获取当前线程
                    System.out.println(t.getName()+"正在执行一个任务");
                    Thread.sleep(1000);//线程休眠1秒
                    System.out.println(t.getName()+"执行完毕");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            };
            threadPool.execute(r);//执行任务
            threadPool.shutdown();//线程池关闭
        }

    }
}
